package com.yh.security;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 权限不足返回信息
 * @author yh
 */
@Component
public class JwtAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException {
        String SESSION_CODE = "{"+'"'+"code"+'"'+":403,"+'"'+"messgae"+'"'+":"+'"'+"权限不足"+'"'+","+'"'+"data"+'"'+":null"+""+'}';
        response.setStatus(200);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter printWriter = response.getWriter();
        String body = SESSION_CODE;
        printWriter.write(body);
        printWriter.flush();
    }
}
